
/**
 * 页面加载事件
 */
function initData(){
    //读取角色
    var arr = JSON.parse(sessionStorage.getItem("userRoleList"));
    //下拉列表父元素
    var fu = document.getElementById("role");
    for (var item of arr) {
       var opt = document.createElement("option");
       opt.value=item.roleName;
       opt.innerText = item.roleName;
       fu.appendChild(opt);
    }
    
    //读取权限
    var arr = JSON.parse(sessionStorage.getItem("permissionList"));
    //模块名
    var mkm = arr[0].mkm;
    //父节点
    var fu = document.getElementsByClassName("per")[0];
    for (var item of arr) {
        //判断模块名是否一致
        if(item.mkm != mkm){
            fu.appendChild(document.createElement("br"));
            mkm = item.mkm;
        }

        var span = document.createElement("span");
        span.innerHTML="<input type='checkbox' class='pers' value='"+item.permissName+"' /> "+item.permissName+" &nbsp;&nbsp;"
        //渲染节点
        fu.appendChild(span);
    }
}

/**
 * 根据角色显示权限
 */
function seachPermiss(){
    //选中角色
    var roleArr = document.getElementById("role").children;
    //角色名
    var roleName;
    for(var i=0; i<roleArr.length; i++){
        if(roleArr[i].selected){
            roleName = roleArr[i].value;
            break;
        }
    }
    if(roleName != "-1"){
        //角色对应权限
        var arr = JSON.parse(sessionStorage.getItem("userRoleList"));
        //权限
        var permiss;
        for(var i=0; i<arr.length; i++){
            if(arr[i].roleName == roleName){
                permiss = arr[i].glqx;
                break;
            }
        }
        console.log(permiss);
        //勾选权限
        var chk = document.getElementsByClassName("pers");
        for(var i=0; i<chk.length; i++){
            chk[i].checked=false;
            //判断页面中的权限名是否在session角色对应权限的数组中
            if(permiss.includes(chk[i].value)){
                chk[i].checked=true;
            }
        }
    }else{
        //勾选权限
        var chk = document.getElementsByClassName("pers");
        for(var i=0; i<chk.length; i++){
            chk[i].checked=false;
        }
    }
}

/**
 * 保存角色的新权限
 */
function permissSave(){
    //选中角色
    var roleArr = document.getElementById("role").children;
    //角色名
    var roleName;
    for(var i=0; i<roleArr.length; i++){
        if(roleArr[i].selected){
            roleName = roleArr[i].value;
            break;
        }
    }
    //获取勾选的新权限
    //勾选权限
    var chk = document.getElementsByClassName("pers");
    //新权限数组
    var newPermiss=[];
    for(var i=0; i<chk.length; i++){
        if(chk[i].checked){
            newPermiss.push(chk[i].value);
        }
    }
    console.log(newPermiss);

    //角色对应权限
    var arr = JSON.parse(sessionStorage.getItem("userRoleList"));
    for(var i=0; i<arr.length; i++){
        if(arr[i].roleName == roleName){
            //清空选中角色的旧权限，添加新权限
            arr[i].glqx = newPermiss;
            break;
        }
    }
    sessionStorage.setItem("userRoleList",JSON.stringify(arr));

}

/**
 * 权限控制跳转订单页
 */
function jumpToBill(){
    //获取登录名
    var loginName = sessionStorage.getItem("loginName");
    //根据登录名获取角色
    var roleName;
    var arr = JSON.parse(sessionStorage.getItem("userList"));
    for (var item of arr) {
        if(item.yhxm == loginName){
            roleName = item.yhlx;
            break;
        }
    }
    //根据角色获取权限
    var permiss = [];
    var arr = JSON.parse(sessionStorage.getItem("userRoleList"));
    for (var item of arr) {
        if(item.roleName == roleName){
            permiss = item.glqx;
            break;
        }
    }
    //判断权限中是否包含“订单模块管理”
    for (var item of permiss) {
        if(item == "订单模块管理"){
            location.href="billList.html";
        }
    }

}
